package com.ilancuo.java8;

import java.util.*;
import java.util.function.Supplier;
import java.util.stream.Collectors;

public class CollectorsTest {

    private static List<String> servers = Arrays.asList("Felordcn", "Tomcat", "Jetty", "Undertow", "Resin");

    public static void main(String[] args) {
//        testJoining();
//        testCollectingAndThen();
//        testGroupBy();
//        testMaxBy();
//        testSum();
//        testSummarizing();
        testMapping();
    }

    private static void testJoining() {
        String result = servers.stream().collect(Collectors.joining()); //FelordcnTomcatJettyUndertowResin
        String result1 = servers.stream().collect(Collectors.joining(", ")); //Felordcn, Tomcat, Jetty, Undertow, Resin
        String result2 = servers.stream().collect(Collectors.joining(", ", "[", "]")); //[Felordcn, Tomcat, Jetty, Undertow, Resin]
        System.out.println(result);
        System.out.println(result1);
        System.out.println(result2);
    }

    private static void testCollectingAndThen() {
        String result = servers.stream().collect(Collectors.collectingAndThen(Collectors.joining(","), String::toUpperCase)); //FELORDCN,TOMCAT,JETTY,UNDERTOW,RESIN
        System.out.println(result);
    }

    private static void testGroupBy() {
        Map<Integer, List<String>> collect = servers.stream().collect(Collectors.groupingBy(String::length));
        System.out.println(collect);
        Map<Integer, Set<String>> collect1 = servers.stream().collect(Collectors.groupingBy(String::length, Collectors.toSet()));
        System.out.println(collect1);

        // 同步容器，线程安全
        Supplier<Map<Integer, Set<String>>> mapSupplier = HashMap::new;
        Map<Integer, Set<String>> collect2 = servers.stream().collect(Collectors.groupingBy(String::length, mapSupplier, Collectors.toSet()));
        System.out.println(collect2);

        Supplier<Map<Integer, Set<String>>> mapSupplier1 = () -> Collections.synchronizedMap(new HashMap<>());
        Map<Integer, Set<String>> collect3 = servers.stream().collect(Collectors.groupingBy(String::length, mapSupplier1, Collectors.toSet()));
        System.out.println(collect3);
    }

    private static void testMaxBy() {
        Optional<String> collect = servers.stream().collect(Collectors.minBy(Comparator.comparingInt(String::length)));
        System.out.println(collect);
    }

    private static void testSum() {
        Integer collect = servers.stream().collect(Collectors.summingInt(s -> s.length()));
        System.out.println(collect);
    }

    private static void testSummarizing() {
        DoubleSummaryStatistics collect = servers.stream().collect(Collectors.summarizingDouble(String::length));
        System.out.println(collect.toString());
    }

    private static void testMapping() {
        List<String> collect = servers.stream().collect(Collectors.mapping(s -> s.substring(1), Collectors.toList()));
        System.out.println(collect);
        List<String> collect1 = servers.stream().map(s -> s.substring(1)).collect(Collectors.toList());
        System.out.println(collect1);
    }
}
